import pygame,time,random
from pygame.locals import  * 
screen = pygame.display.set_mode((480,800))
pygame.init()

bg = pygame.image.load("background.png")

# 加载素材
# speed越大速度越快，qty越大数量越少，blood_re越大血量越大
# user
# fly
hero0 = pygame.image.load("hero0.png")
hero1 = pygame.image.load("hero1.png")
hero_down = pygame.image.load("hero_down.png")
heroX = 200
heroY = 400

# bullet
bullet = pygame.image.load("bullet1.png")
bulletX = []
bulletY = []
bullet_speed = 10
bullet_qty = 5

# bullet_shape
flag = False

# computer
# lv1
enemy1 = pygame.image.load("enemy1.png")
enemy1_down1 = pygame.image.load("enemy1_down1.png")
enemy1_down2 = pygame.image.load("enemy1_down2.png")
enemy1_down3 = pygame.image.load("enemy1_down3.png")
enemy1_x = []
enemy1_y = []
enemy1_speed  = 2
enemy1_qty = 20

# lv2
enemy2 = pygame.image.load("enemy2.png")
enemy2_down1 = pygame.image.load("enemy2_down1.png")
enemy2_down2 = pygame.image.load("enemy2_down2.png")
enemy2_down3 = pygame.image.load("enemy2_down3.png")
enemy2_x = []
enemy2_y = []
enemy2_blood =  []
enemy2_speed  = 1
enemy2_qty = 120
enemy2_blood_re = 30

#lv3
enemy3 = pygame.image.load("enemy3.png")
enemy3_down1 = pygame.image.load("enemy3_down1.png")
enemy3_down2 = pygame.image.load("enemy3_down2.png")
enemy3_down3 = pygame.image.load("enemy3_down3.png")
enemy3_x = []
enemy3_y = []
enemy3_blood = []
enemy3_speed  = 0.3
enemy3_qty = 1600
enemy3_blood_re = 2400

# game start
gamestart = pygame.image.load("start.png")
gamestart = pygame.transform.scale(gamestart,(480,800))
screen.blit(gamestart,(0,0))
pygame.display.update()
time.sleep(1)
#  game over
gameover = pygame.image.load("gameover.png")
gameover = pygame.transform.scale(gameover,(480,800))
isgameover = 0

# 计数器
num = 1

# score
font = pygame.font.Font("simsun.ttc",40)
font2 = pygame.font.Font("cuyabra.otf",40)
text = font.render("得分：",True,(255,255,255))
score = 0

# 游戏内循环
while 1:
   pos = pygame.mouse.get_pos()
   screen.blit(bg,(0,0))
   text_score = font2.render(str(score),True,(255,255,255))
   screen.blit(text,(10,10))
   screen.blit(text_score,(120,15))
   # user
   heroX = pos[0]-hero0.get_width()/2
   heroY = pos[1]-hero0.get_height()/2

   if isgameover == 0: 
      if num%2 == 0:
         screen.blit(hero1,(heroX,heroY))
      else:
         screen.blit(hero0,(heroX,heroY))
      num += 1
      time.sleep(0.01)

      # computer
      # lv1
      if num % enemy1_qty == 0:
         enemy1_x.append(random.randint(0,480-enemy1.get_width()))
         enemy1_y.append(0)
      for i in range(len(enemy1_x)):
         screen.blit(enemy1,(enemy1_x[i],enemy1_y[i]))

      for i in range(len(enemy1_x)):
         enemy1_y[i] += enemy1_speed
         if enemy1_y[i] > 800:
            isgameover = 1
            break
      
      # lv2
      if num % enemy2_qty == 0:
         enemy2_x.append(random.randint(0,480-enemy2.get_width()))
         enemy2_y.append(0)
         enemy2_blood.append(enemy2_blood_re)
      for i in range(len(enemy2_x)):
         screen.blit(enemy2,(enemy2_x[i],enemy2_y[i]))

      for i in range(len(enemy2_x)):
         enemy2_y[i] += enemy2_speed
         if enemy2_y[i] > 800:
            isgameover = 1
            break

      # lv3
      if num % enemy3_qty == 0:
         enemy3_x.append(random.randint(0,480-enemy3.get_width()))
         enemy3_y.append(0)
         enemy3_blood.append(enemy3_blood_re)
      for i in range(len(enemy3_x)):
         screen.blit(enemy3,(enemy3_x[i],enemy3_y[i]))

      for i in range(len(enemy3_x)):
         enemy3_y[i] += enemy3_speed
         if enemy3_y[i] > 800:
            isgameover = 1
            break

      # bullet
      if num % bullet_qty == 0:
         if flag == False:
            bulletX.append(heroX + hero0.get_width() / 2 - bullet.get_width() / 2) 
            bulletY.append(heroY - bullet.get_height())
         elif flag == True:
            # bullet_one
            bulletX.append(heroX + hero0.get_width() / 4 -bullet.get_width() - 5)
            bulletY.append(heroY + hero0.get_width() / 4)
            # bullet_two
            bulletX.append(heroX + 3 * hero0.get_width() / 4 +5)
            bulletY.append(heroY + hero0.get_height() / 4)


      for i in range(len(bulletX)):
         screen.blit(bullet,(bulletX[i],bulletY[i]))

      for i in range(len(bulletX)):
         bulletY[i] -= bullet_speed
         if bulletY[i] < 0:
            del bulletX[i]
            del bulletY[i]
            break

      # 子弹是否打中敌机
      # lv1
      for i in range(len(bulletX)):
         for j in range(len(enemy1_x)):
            # x
            if bulletX[i] > enemy1_x[j] - bullet.get_width() and bulletX[i] < enemy1_x[j] + enemy1.get_width():
               # y
               if bulletY[i] > enemy1_y[j] - bullet.get_height() and bulletY[i] < enemy1_y[j] + enemy1.get_height():
                  print("Enemy1 boom!")
                  screen.blit(enemy1_down1,(enemy1_x[j],enemy1_y[j]))
                  screen.blit(enemy1_down2,(enemy1_x[j],enemy1_y[j]))
                  screen.blit(enemy1_down3,(enemy1_x[j],enemy1_y[j]))
                  del enemy1_x[j]
                  del enemy1_y[j]
                  score += 1
                  break
      
      # lv2
      for i in range(len(bulletX)):
         for j in range(len(enemy2_x)):
            # x
            if bulletX[i] > enemy2_x[j] - bullet.get_width() and bulletX[i] < enemy2_x[j] + enemy2.get_width():
               # y
               if bulletY[i] > enemy2_y[j] - bullet.get_height() and bulletY[i] < enemy2_y[j] + enemy2.get_height():
                  enemy2_blood[j] -= 1
                  if enemy2_blood[j] <= 0:
                     print("Enemy2 boom!")
                     screen.blit(enemy2_down1,(enemy2_x[j],enemy2_y[j]))
                     screen.blit(enemy2_down2,(enemy2_x[j],enemy2_y[j]))
                     screen.blit(enemy2_down3,(enemy2_x[j],enemy2_y[j]))
                     del enemy2_x[j]
                     del enemy2_y[j]
                     del enemy2_blood[j]
                     score += 5
                     break
      
      # lv3
      for i in range(len(bulletX)):
         for j in range(len(enemy3_x)):
            # x
            if bulletX[i] > enemy3_x[j] - bullet.get_width() and bulletX[i] < enemy3_x[j] + enemy3.get_width():
               # y
               if bulletY[i] > enemy3_y[j] - bullet.get_height() and bulletY[i] < enemy3_y[j] + enemy3.get_height():
                  enemy3_blood[j] -= 1
                  if enemy3_blood[j] <= 0:
                     print("Enemy3 boom!")
                     screen.blit(enemy3_down1,(enemy3_x[j],enemy3_y[j]))
                     screen.blit(enemy3_down2,(enemy3_x[j],enemy3_y[j]))
                     screen.blit(enemy3_down3,(enemy3_x[j],enemy3_y[j]))
                     del enemy3_x[j]
                     del enemy3_y[j]
                     del enemy3_blood[j]
                     score += 10
                     break

      # user_died
      # lv1
      for i in range(len(enemy1_x)):
         # x
         if enemy1_x[i] > heroX - enemy1.get_width() and enemy1_x[i] < heroX + hero0.get_width():
            # y
            if  enemy1_y[i] > heroY - enemy1.get_height() and enemy1_y[i] < heroY + hero0.get_height():
               screen.blit(hero_down,(heroX,heroY))
               isgameover = 1

      # lv2
      for i in range(len(enemy2_x)):
         # x
         if enemy2_x[i] > heroX - enemy2.get_width() and enemy2_x[i] < heroX + hero0.get_width():
            # y
            if  enemy2_y[i] > heroY - enemy2.get_height() and enemy2_y[i] < heroY + hero0.get_height():
               screen.blit(hero_down,(heroX,heroY))
               isgameover = 1

      # lv3
      for i in range(len(enemy3_x)):
         # x
         if enemy3_x[i] > heroX - enemy3.get_width() and enemy3_x[i] < heroX + hero0.get_width():
            # y
            if  enemy3_y[i] > heroY - enemy3.get_height() and enemy3_y[i] < heroY + hero0.get_height():
               screen.blit(hero_down,(heroX,heroY))
               isgameover = 1

   elif isgameover == 1:
      screen.blit(gameover,(0,0))
   
   # result all
   else:
      num = 0
      # user
      heroX = 200
      heroY = 400
      # computer
      enemy1_x = []
      enemy1_y = []
      enemy2_x = []
      enemy2_y = []
      enemy3_x = []
      enemy3_y = []
      # bullet
      bulletX = []
      bulletY = []
      # bullet_shape
      flag = False
      # score
      score = 0
      # resuit
      isgameover = 0

   pygame.display.update()

   # close
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         pygame.quit()
         exit()

      # keyboard
      if event.type == KEYDOWN:
         if event.key == K_q:
            flag = True 
         if event.key == K_e:
            flag = False
         if event.key == K_SPACE:
            if isgameover  == 1:
               isgameover = 2
